package com.ghxxzx.dagl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghxxzx.dagl.entity.*;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.*;
import com.ghxxzx.dagl.service.DalrService;
import com.ghxxzx.dagl.utils.*;
import com.ghxxzx.dagl.vo.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.ngram.NGramTokenizer;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class DalrServiceImpl implements DalrService {
    @Autowired
    private DaxxMapper daxxMapper;
    @Autowired
    private DalxMapper dalxMapper;
    @Autowired
    private ClxxMapper clxxMapper;
    @Autowired
    private DaxxWsMapper daxxWsMapper;
    @Autowired
    private ClPathMapper clPathMapper;
    //    @Autowired
//    private DaxxWsMapper daxxWsMapper;
    @Autowired
    private WsFlag wsFlag;
    @Autowired
    private DaxxRgMapper daxxRgMapper;
    @Autowired
    private DaxxWsRgMapper daxxWsRgMapper;
    @Autowired
    private ClxxRgMapper clxxRgMapper;
    @Autowired
    private RgshMapper rgshMapper;
    @Autowired
    private PathUtils pathUtils;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GryfKjMapper gryfKjMapper;
    @Autowired
    private GryfKjRgMapper gryfKjRgMapper;
    @Autowired
    private LuceneUtiles luceneUtiles;
    @Autowired
    private SyMoreMapper syMoreMapper;

    @Value("${lucene.index}")
    private String indexPath;

    @Override
    public ResultEntity queryDayilbWs() {
        List<DaylbVo> daYlb = getDaYlbWsVo();
        return ResultEntity.ok(daYlb);
    }

    @Override
    public ResultEntity queryDayilb() {
        List<DaylbVo> daYlb = getDaYlbVo();
        return ResultEntity.ok(daYlb);
    }

    public List<DaylbVo> getDaYlbWsVo() {
        List<Dalx> allfirst = dalxMapper.findAllfirstWs();
        ArrayList<Dalx> all = new ArrayList<>();
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        for (Dalx dalx : allfirst) {
            Integer lx = hashMap.get(dalx.getId());
            if (lx == 3) {
                if ((!dalx.getCode().equalsIgnoreCase("ws")) && (!dalx.getCode().equalsIgnoreCase("kj"))) {
                    all.add(dalx);
                }
            }
        }
        List<DaylbVo> daylbVos = new ArrayList<>();
        QueryWrapper<DaxxWs> wrapper = new QueryWrapper<>();
        wrapper.select("nf", "type_id");
        List<DaxxWs> daxxWs = daxxWsMapper.selectList(wrapper);
        if (daxxWs == null) {
            daxxWs = new ArrayList<>();
        }
        String maxYear = daxxWsMapper.findByMaxYear();
        if (maxYear == null) {
            maxYear = "2020";
        }
        Integer max = Integer.parseInt(maxYear);
        for (Dalx dalx : all) {
            HashMap<Integer, YlbLb> map = new HashMap<>();
            Integer qq = max;
            for (int i = 0; i < 9; i++) {
                if (i <= 6) {
                    map.put(i + 1, new YlbLb(qq, qq, 0));
                    qq--;
                } else if (i == 7) {
                    Integer w = (qq - 10);
                    map.put(i + 1, new YlbLb(w, qq, 0));
                    qq = w;
                    qq--;
                } else {
                    map.put(i + 1, new YlbLb(0, qq, 0));
                }
            }
            getYlbWsCount(map, dalx, all, daxxWs);
            ArrayList<TypeYear> typeYears = new ArrayList<>();
            for (Integer integer : map.keySet()) {
                YlbLb ylbLb = map.get(integer);
                if (integer <= 7) {
                    typeYears.add(new TypeYear(ylbLb.getStop() + "", ylbLb.getConunt()));
                } else if (integer == 8) {
                    typeYears.add(new TypeYear(ylbLb.getStart() + "-" + ylbLb.getStop() + "", ylbLb.getConunt()));
                } else {
                    typeYears.add(new TypeYear(ylbLb.getStop() + "年以前", ylbLb.getConunt()));
                }

            }
            daylbVos.add(new DaylbVo(dalx.getDalxName(), dalx.getId(), typeYears));
            map = null;
        }
        return daylbVos;
    }

    public boolean gtFlag(int startyear, String year) {
        if (StringUtils.isEmpty(year)) {
            year = "0";
        }
        int i = Integer.parseInt(year);
        if (i >= startyear) {
            return true;
        } else {
            return false;
        }
    }

    public boolean ltFlag(int stopyear, String year) {
        if (StringUtils.isEmpty(year)) {
            year = "0";
        }
        int i = Integer.parseInt(year);
        if (i <= stopyear) {
            return true;
        } else {
            return false;
        }
    }

    public void getYlbWsCount(HashMap<Integer, YlbLb> hashMap, Dalx dalx, List<Dalx> dalxList, List<DaxxWs> daxxWsList) {
        for (DaxxWs daxxWs : daxxWsList) {
            String nf = null;
            if (daxxWs.getNf() != null) {
                nf = daxxWs.getNf() + "";
            }
            if (dalx.getId().intValue() == daxxWs.getTypeId().intValue()) {
                setDaylbHasMap(hashMap, nf);
            }
        }

        List<Dalx> dalxes = new ArrayList<>();
        for (Dalx dalx1 : dalxList) {
            if (dalx.getId().intValue() == dalx1.getParentId().intValue()) {
                dalxes.add(dalx1);
            }
        }
        if (dalxes.size() > 0) {
            for (Dalx dalx1 : dalxes) {
                getYlbWsCount(hashMap, dalx1, dalxList, daxxWsList);
            }
        }
    }

    public void setDaylbHasMap(HashMap<Integer, YlbLb> hasMap, String year) {
        for (Integer integer : hasMap.keySet()) {
            if (gtFlag(hasMap.get(integer).getStart(), year) && ltFlag(hasMap.get(integer).getStop(), year)) {
                YlbLb ylbLb = hasMap.get(integer);
                ylbLb.setConunt(ylbLb.getConunt() + 1);
                hasMap.put(integer, ylbLb);
                break;
            }
        }
//        if (gtFlag(hasMap.get(1).getStart(), year) && ltFlag(hasMap.get(1).getStart(), year)) {
//            YlbLb ylbLb = hasMap.get(1);
//            ylbLb.setConunt(ylbLb.getConunt()+1);
//            hasMap.put(1, ylbLb);
//        } else if (hasMap.get(1).getStart(),year) && ltFlag(hasMap.get(1).getStart(), year)) {
//            YlbLb ylbLb = hasMap.get(2);
//            hasMap.put(2, (integer + 1));
//        } else if (gtFlag(2018, year) && ltFlag(2018, year)) {
//            YlbLb ylbLb = hasMap.get(3);
//            hasMap.put(3, (integer + 1));
//        } else if (gtFlag(2017, year) && ltFlag(2017, year)) {
//            YlbLb ylbLb = hasMap.get(4);
//            hasMap.put(4, (integer + 1));
//        } else if (gtFlag(2016, year) && ltFlag(2016, year)) {
//            YlbLb ylbLb = hasMap.get(5);
//            hasMap.put(5, (integer + 1));
//        } else if (gtFlag(2015, year) && ltFlag(2015, year)) {
//            YlbLb ylbLbr = hasMap.get(6);
//            hasMap.put(6, (integer + 1));
//        } else if (gtFlag(2014, year) && ltFlag(2014, year)) {
//            YlbLb ylbLb = hasMap.get(7);
//            hasMap.put(7, (integer + 1));
//        } else if (gtFlag(2003, year) && ltFlag(2013, year)) {
//            YlbLb ylbLb = hasMap.get(8);
//            hasMap.put(8, (integer + 1));
//        } else if (gtFlag(0, year) && ltFlag(2002, year)) {
//            YlbLb ylbLb = hasMap.get(9);
//            hasMap.put(9, (integer + 1));
//        }
    }

    public List<DaylbVo> getDaYlbVo() {
        List<Dalx> allfirst = dalxMapper.findAllfirst();
        ArrayList<Dalx> all = new ArrayList<>();
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        for (Dalx dalx : allfirst) {
            Integer lx = hashMap.get(dalx.getId());
            if (lx == 1 || lx == 2) {
                if ((!dalx.getCode().equalsIgnoreCase("ws")) && (!dalx.getCode().equalsIgnoreCase("kj"))) {
                    all.add(dalx);
                }
            }
        }
        List<DaylbVo> daylbVos = new ArrayList<>();
        QueryWrapper<Daxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("year", "type_id");
        List<Daxx> daxxes = daxxMapper.selectList(queryWrapper);
        if (daxxes == null) {
            daxxes = new ArrayList<>();
        }
        QueryWrapper<GryfKj> wrapper = new QueryWrapper<>();
        wrapper.select("year", "type_id");
        List<GryfKj> gryfKjList = gryfKjMapper.selectList(wrapper);

        String maxYear = gryfKjMapper.findByMaxYear();
        if (maxYear == null) {
            maxYear = "2020";
        }
        String maxYear2 = daxxMapper.findByMaxYear();
        if (maxYear2 == null) {
            maxYear2 = "2020";
        }
        Integer max = Integer.max(Integer.parseInt(maxYear), Integer.parseInt(maxYear2));
        for (Dalx dalx : all) {
            HashMap<Integer, YlbLb> map = new HashMap<>();
            Integer qq = max;
            for (int i = 0; i < 9; i++) {
                if (i <= 6) {
                    map.put(i + 1, new YlbLb(qq, qq, 0));
                    qq--;
                } else if (i == 7) {
                    Integer w = (qq - 10);
                    map.put(i + 1, new YlbLb(w, qq, 0));
                    qq = w;
                    qq--;
                } else {
                    map.put(i + 1, new YlbLb(0, qq, 0));
                }

            }
            getYlbCount(map, dalx, all, daxxes, gryfKjList);
            ArrayList<TypeYear> typeYears = new ArrayList<>();
            for (Integer integer : map.keySet()) {
                YlbLb ylbLb = map.get(integer);
                if (integer <= 7) {
                    typeYears.add(new TypeYear(ylbLb.getStop() + "", ylbLb.getConunt()));
                } else if (integer == 8) {
                    typeYears.add(new TypeYear(ylbLb.getStart() + "-" + ylbLb.getStop() + "", ylbLb.getConunt()));
                } else {
                    typeYears.add(new TypeYear(ylbLb.getStop() + "年以前", ylbLb.getConunt()));
                }

            }
            daylbVos.add(new DaylbVo(dalx.getDalxName(), dalx.getId(), typeYears));
            map = null;
        }
        return daylbVos;
    }

    //科技
    public void getYlbCount(HashMap<Integer, YlbLb> hashMap, Dalx dalx, List<Dalx> dalxList, List<Daxx> daxxList, List<GryfKj> gryfKjList) {
        for (Daxx daxx : daxxList) {
            String nf = null;
            if (daxx.getYear() != null) {
                nf = daxx.getYear() + "";
            }
            if (dalx.getId().intValue() == daxx.getTypeId().intValue()) {
                setDaylbHasMap(hashMap, nf);
            }
        }
        for (GryfKj gryfKj : gryfKjList) {
            String nf = "";
            if (gryfKj.getYear() != null) {
                nf = gryfKj.getYear();
            }
            if (dalx.getId().intValue() == gryfKj.getTypeId().intValue()) {
                setDaylbHasMap(hashMap, nf);
            }
        }
        List<Dalx> dalxes = new ArrayList<>();
        for (Dalx dalx1 : dalxList) {
            if (dalx.getId().intValue() == dalx1.getParentId().intValue()) {
                dalxes.add(dalx1);
            }
        }
        if (dalxes.size() > 0) {
            for (Dalx dalx1 : dalxes) {
                getYlbCount(hashMap, dalx1, dalxList, daxxList, gryfKjList);
            }
        }
    }

    public String getIdString(Integer wdlx) {
        Set<Integer> ids = new HashSet<>();
        ids.add(wdlx);
        handlerLxId(ids, wdlx);
        ArrayList<Integer> typeids = new ArrayList<>();
        for (Integer id : ids) {
            typeids.add(id);
        }
        String ss = "";
        for (int i = 0; i < typeids.size(); i++) {
            if (i == (typeids.size() - 1)) {
                ss += typeids.get(i);
            } else {
                ss += typeids.get(i) + ",";
            }
        }
        return ss;
    }

    /**
     * @param keyword
     * @param dalx
     * @param sort
     * @param pagesize
     * @param pagenumber
     * @return
     */
    @Override
    public ResultEntity queryDaxx(String keyword, Integer dalx, String sort, Integer pagesize, Integer pagenumber, String year) throws Exception {
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(pagenumber);
        pageResult.setPageSize(pagesize);
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer lx = hashMap.get(dalx);
        if (lx == 3) {
            luceneUtiles.searchDaxxWsPageMl(dalx, keyword, 0, 9999, new PageQuery(pagenumber, pagesize), sort, pageResult, year);
        } else if (lx == 1) {
            luceneUtiles.searchDaxxPageMl(dalx, keyword, 0, 9999, new PageQuery(pagenumber, pagesize), sort, pageResult, year);
        } else if (lx == 2) {
            luceneUtiles.searchGryfPageMl(dalx, keyword, 0, 9999, new PageQuery(pagenumber, pagesize), sort, pageResult, year);
        }
        return ResultEntity.ok(pageResult);
    }

    private void handlerLxId(Set<Integer> ids, Integer dalx) {
        //第一级子节点
        List<Dalx> dalxes = dalxMapper.findParentIdEq(dalx);
        if (dalxes == null || dalxes.size() == 0) {
            return;
        }
        Set<Integer> collect = dalxes.stream().map(Dalx::getId).collect(Collectors.toSet());
        ids.addAll(collect);
        for (Integer id : collect) {
            handlerLxId(ids, id);
        }
    }

    @Override
    public ResultEntity dabr(Daxx daxx, User user) throws CloneNotSupportedException {
        Integer typeId = daxx.getTypeId();
        Dalx dalx = dalxMapper.selectById(typeId);
        daxx.setTypeName(dalx.getDalxName());
        if (daxx.getId() != null) {
            DaxxRg byDabh = new DaxxRg();
            BeanUtils.copyProperties(daxx, byDabh);
            byDabh.setYear(Integer.parseInt(daxx.getYear().trim()));
            ResultEntity resultEntity = updateda(byDabh);
            return resultEntity;
        }
        Daxx dabh = daxxMapper.findByDabh(daxx.getDabh());
        if (dabh != null) {
            throw new JyException("该档案编号已录入");
        }
        DaxxRg byDabh = daxxRgMapper.findByDabh(daxx.getDabh());
        if (byDabh != null) {
            throw new JyException("该档案编号已入馆");
        }
        DaxxRg daxxRg = new DaxxRg();
        daxxRg.setYear(Integer.parseInt(daxx.getYear()));
        BeanUtils.copyProperties(daxx, daxxRg);
        daxxRgMapper.insert(daxxRg);
        Rgsh rgsh = new Rgsh(daxxRg.getDabh(), new Date(), null, user.getId(), user.getName(), null, null, 1, 1, daxxRg.getXmmc(), daxxRg.getDtslbh(), daxxRg.getJsdw());
        rgshMapper.insert(rgsh);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity updateda(DaxxRg daxxRg) {
        QueryWrapper<Rgsh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dabh",daxxRg.getDabh());
        Rgsh selectOne = rgshMapper.selectOne(queryWrapper);
        if (selectOne.getZt()!=1){
            throw new JyException("已提交或已不存在无法继续修改了");
        }
        DaxxRg byId = daxxRgMapper.selectById(daxxRg.getId());
        if (!byId.getDabh().equalsIgnoreCase(daxxRg.getDabh())) {
            DaxxRg byDabh = daxxRgMapper.findByDabh(daxxRg.getDabh());
            if (byDabh != null) {
                throw new JyException("该档案编号已录入");
            }
            Daxx dabh = daxxMapper.findByDabh(daxxRg.getDabh());
            if (dabh != null) {
                throw new JyException("该档案编号已入馆");
            }

            UpdateWrapper<ClxxRg> clxxRgUpdateWrapper = new UpdateWrapper<>();
            clxxRgUpdateWrapper.eq("dabh", byId.getDabh());
            clxxRgUpdateWrapper.set("dabh", daxxRg.getDabh());
            clxxRgMapper.update(new ClxxRg(), clxxRgUpdateWrapper);
            UpdateWrapper<Rgsh> rgshUpdateWrapper = new UpdateWrapper<>();
            rgshUpdateWrapper.eq("dabh", byId.getDabh());
            rgshUpdateWrapper.set("dabh", daxxRg.getDabh());
            rgshMapper.update(new Rgsh(), rgshUpdateWrapper);
        }
        daxxRgMapper.updateById(daxxRg);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity updatedaWs(DaxxWsRg daxxWsRg) {
        QueryWrapper<Rgsh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dabh",daxxWsRg.getDabh());
        Rgsh rgsh = rgshMapper.selectOne(queryWrapper);
        if (rgsh.getZt()!=1){
            throw new JyException("已提交或已不存在无法继续修改了");
        }
        DaxxWsRg byId = daxxWsRgMapper.selectById(daxxWsRg.getId());
        if (!byId.getDabh().equalsIgnoreCase(daxxWsRg.getDabh())) {
            DaxxWs byDabh = daxxWsMapper.findByDabh(daxxWsRg.getDabh());
            if (byDabh != null) {
                throw new JyException("该档案编号已入馆");
            }
            DaxxWsRg daxxWsRg1 = daxxWsRgMapper.findByDabh(daxxWsRg.getDabh());
            if (daxxWsRg1 != null) {
                throw new JyException("该档案编号已录入");
            }

            UpdateWrapper<ClxxRg> clxxRgUpdateWrapper = new UpdateWrapper<>();
            clxxRgUpdateWrapper.eq("dabh", byId.getDabh());
            clxxRgUpdateWrapper.set("dabh", daxxWsRg.getDabh());
            clxxRgMapper.update(new ClxxRg(), clxxRgUpdateWrapper);
            UpdateWrapper<Rgsh> rgshUpdateWrapper = new UpdateWrapper<>();
            rgshUpdateWrapper.eq("dabh", byId.getDabh());
            rgshUpdateWrapper.set("dabh", daxxWsRg.getDabh());
            rgshMapper.update(new Rgsh(), rgshUpdateWrapper);
        }
        daxxWsRgMapper.updateById(daxxWsRg);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity deleteda(String dabh) {
//        String code = wsFlag.getCode(dabh);
//        Integer byCode = dalxMapper.findByCode(code);
        Integer byCode = wsFlag.getTypeId(dabh);
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer lx = hashMap.get(byCode);
        if (lx == 1) {
            DaxxRg daxxRg = daxxRgMapper.findByDabh(dabh);
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(daxxRg.getDabh());
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            if (clxxRgs.size() > 0) {
                throw new JyException("请先删除相关联的档案材料信息");
            }
            daxxRgMapper.deleteById(daxxRg.getId());
        } else if (lx == 2) {
            GryfKj gryfKj = gryfKjMapper.findByDabh(dabh);
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(dabh);
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            if (clxxRgs.size() > 0) {
                throw new JyException("请先删除相关联的档案材料信息");
            }
            daxxRgMapper.deleteById(gryfKj.getId());
        }

        UpdateWrapper<Rgsh> wrapper = new UpdateWrapper<>();
        wrapper.eq("dabh", dabh);
        rgshMapper.delete(wrapper);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity deletecl(Integer id) {
        ClxxRg clxxRg = clxxRgMapper.selectById(id);
        String dacl = pathUtils.getPath();
        String pdfPath = dacl + "/" + clxxRg.getTypeName() + "/" + clxxRg.getDabh() + "/" + clxxRg.getWjxh() + clxxRg.getClmc() + ".pdf";
        String path = dacl + "/" + clxxRg.getTypeName() + "/" + clxxRg.getDabh() + "/" + clxxRg.getWjxh() + clxxRg.getClmc();
        DeleteFileUtils.deleteFile(pdfPath);
        DeleteFileUtils.deleteDirectory(path);
        clxxRgMapper.deleteById(id);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity deletedaWs(Integer id) {
        DaxxWsRg daxxWsRg = daxxWsRgMapper.selectById(id);
        List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(daxxWsRg.getDabh());
        if (clxxRgs == null) {
            clxxRgs = new ArrayList<>();
        }
        if (clxxRgs.size() > 0) {
            throw new JyException("请先删除相关联的档案材料信息");
        }
        daxxWsRgMapper.deleteById(id);
        UpdateWrapper<Rgsh> wrapper = new UpdateWrapper<>();
        wrapper.eq("dabh", daxxWsRg.getDabh());
        rgshMapper.delete(wrapper);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity updatecl(ClxxRg clxxRg) {
        QueryWrapper<Rgsh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dabh",clxxRg.getDabh());
        Rgsh rgsh = rgshMapper.selectOne(queryWrapper);
        if (rgsh.getZt()!=1){
            throw new JyException("已提交或已不存在无法继续修改了");
        }
        ClxxRg byId = clxxRgMapper.selectById(clxxRg.getId());
        if (!byId.getDabh().equalsIgnoreCase(clxxRg.getDabh())) {
            UpdateWrapper<ClxxRg> clxxRgUpdateWrapper = new UpdateWrapper<>();
            clxxRgUpdateWrapper.eq("dabh", byId.getDabh());
            clxxRgUpdateWrapper.set("dabh", clxxRg.getDabh());
            clxxRgMapper.update(new ClxxRg(), clxxRgUpdateWrapper);
            UpdateWrapper<Rgsh> rgshUpdateWrapper = new UpdateWrapper<>();
            rgshUpdateWrapper.eq("dabh", byId.getDabh());
            rgshUpdateWrapper.set("dabh", clxxRg.getDabh());
            rgshMapper.update(new Rgsh(), rgshUpdateWrapper);
            HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
            Integer lx = hashMap.get(byId.getTypeId());
            if (lx == 3) {
                UpdateWrapper<DaxxWsRg> daxxWsRgUpdateWrapper = new UpdateWrapper<>();
                daxxWsRgUpdateWrapper.eq("dabh", byId.getDabh());
                daxxWsRgUpdateWrapper.set("dabh", clxxRg.getDabh());
                daxxWsRgMapper.update(new DaxxWsRg(), daxxWsRgUpdateWrapper);
            } else if (lx == 1) {
                UpdateWrapper<DaxxRg> daxxRgUpdateWrapper = new UpdateWrapper<>();
                daxxRgUpdateWrapper.eq("dabh", byId.getDabh());
                daxxRgUpdateWrapper.set("dabh", clxxRg.getDabh());
                daxxRgMapper.update(new DaxxRg(), daxxRgUpdateWrapper);
            }
        }
        QueryWrapper<ClxxRg> wrapper = new QueryWrapper<>();
        wrapper.eq("dabh", clxxRg.getDabh());
        wrapper.eq("wjxh", clxxRg.getWjxh());
        wrapper.eq("clmc", clxxRg.getClmc());
        wrapper.ne("id", clxxRg.getId());
        ClxxRg selectOne = clxxRgMapper.selectOne(wrapper);
        if (selectOne != null) {
            throw new JyException("该材料信息已录入，请换个材料名称");
        }
        clxxRgMapper.updateById(clxxRg);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity dabrWs(DaxxWs daxxWs, User user) {
        DaxxWsRg wsRg = new DaxxWsRg();
        if (daxxWs.getId() != null) {
            BeanUtils.copyProperties(daxxWs, wsRg);
            wsRg.setNf(Integer.parseInt(daxxWs.getNf()));
            ResultEntity resultEntity = updatedaWs(wsRg);
            return resultEntity;
//            throw new JyException("该档案编号已录入");
        }
        DaxxWsRg byDabh = daxxWsRgMapper.findByDabh(daxxWs.getDabh());
        if (byDabh != null) {
            throw new JyException("该档案编号已入馆");
        }
        DaxxWs byDabh1 = daxxWsMapper.findByDabh(daxxWs.getDabh());
        if (byDabh1 != null) {
            throw new JyException("该档案编号已录入");
        }
        Integer typeId = daxxWs.getTypeId();
        Dalx dalx = dalxMapper.selectById(typeId);
        daxxWs.setTypeName(dalx.getDalxName());
        BeanUtils.copyProperties(daxxWs, wsRg);
        wsRg.setNf(Integer.parseInt(daxxWs.getNf()));
        daxxWsRgMapper.insert(wsRg);
        Rgsh rgsh = new Rgsh(wsRg.getDabh(), new Date(), null, user.getId(), user.getName(), null, null, 1, 3, wsRg.getWjmc(), null, wsRg.getDwmc());
        rgshMapper.insert(rgsh);
        return ResultEntity.ok();
    }

    public void deleteFile(String path) {
        File file = new File(path);
        //取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //打印文件名
            String name = file.getName();
            System.out.println(name);
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                deleteFile(f.getPath());
            } else {
                f.delete();
            }
        }
        //删除空文件夹  for循环已经把上一层节点的目录清空。
        file.delete();
    }

    @Override
    public ResultEntity uploadWsCl(MultipartFile file, String lb, String dabh) {
        ClPath wsPath = clPathMapper.selectWsPath();
        String path = wsPath.getPath();
        path = path + "/" + lb + "/" + dabh;
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        upload(file, path);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity queryDabhClxx(String dabh) {
        List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(dabh);
        return ResultEntity.ok(clxxRgs);
    }

    @Override
    public ResultEntity getFlagClxx(String clname, String lb, String dabh, String wjxh) {
        ClPath clPath = clPathMapper.selectPath();
        String path = clPath.getPath();
        path = path + "/" + lb + "/" + dabh + "/" + wjxh + clname;
        String pdfPath = path + ".pdf";
        boolean flag = true;
        boolean fileFlag = getFileFlag(path);
        boolean fileFlag1 = getFileFlag(pdfPath);
        if (!fileFlag && !fileFlag1) {
            flag = false;
        }
        return ResultEntity.ok(flag);
    }

    public boolean getFileFlag(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }
        return true;
    }

    @Override
    public ResultEntity uploadWsCls(MultipartFile[] files, String clname, String lb, String dabh, String wjxh) {
        ClPath wsPath = clPathMapper.selectWsPath();
        String path = wsPath.getPath();
        path = path + "/" + lb + "/" + dabh + "/" + wjxh + clname;
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        for (MultipartFile file : files) {
            upload(file, path);
        }
        return ResultEntity.ok();

    }

    public void upload(MultipartFile file, String path) {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream1 = null;
        try {
            inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            int i = originalFilename.lastIndexOf("/");
            String filename = originalFilename.substring(i + 1);
            fileOutputStream1 = new FileOutputStream(path + "/" + filename);
            IOUtils.copy(inputStream, fileOutputStream1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream1 != null) {
                try {
                    fileOutputStream1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public synchronized ResultEntity clbr(Clxx clxx) {
        if (clxx.getClmc()==null||clxx.getWjxh()==null||clxx.getDabh()==null){
            throw new JyException("材料名称，文件序号，档案编号都不能为空");
        }
        QueryWrapper<Rgsh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dabh",clxx.getDabh());
        Rgsh rgsh = rgshMapper.selectOne(queryWrapper);
        if (rgsh.getZt()!=1){
            throw new JyException("已提交或已不存在无法继续修改了");
        }
        Daxx daxx = daxxMapper.findByDabh(clxx.getDabh());
        DaxxWs daxxWs = daxxWsMapper.findByDabh(clxx.getDabh());
        ClxxRg clxxRg = new ClxxRg();
        if (clxx.getId() != null) {
            BeanUtils.copyProperties(clxx, clxxRg);
            ResultEntity updatecl = updatecl(clxxRg);
            return updatecl;
        }
        if (daxx != null || daxxWs != null) {
            throw new JyException("该档案编号档案信息已入馆");
        }
        DaxxRg daxxRg = daxxRgMapper.findByDabh(clxx.getDabh());
        DaxxWsRg daxxWsRg = daxxWsRgMapper.findByDabh(clxx.getDabh());
        if (daxxWsRg == null && daxxRg == null) {
            throw new JyException("该档案编号档案信息没有录入");
        }
        QueryWrapper<ClxxRg> wrapper = new QueryWrapper<>();
        wrapper.eq("dabh", clxx.getDabh());
        wrapper.eq("wjxh", clxx.getWjxh());
        wrapper.eq("clmc", clxx.getClmc());
        ClxxRg selectOne = clxxRgMapper.selectOne(wrapper);
        if (selectOne != null) {
            throw new JyException("该材料信息已录入");
        }
        QueryWrapper<Clxx> clxxQueryWrapper = new QueryWrapper<>();
        clxxQueryWrapper.eq("dabh", clxx.getDabh());
        clxxQueryWrapper.eq("wjxh", clxx.getWjxh());
        clxxQueryWrapper.eq("clmc", clxx.getClmc());
        Clxx clxx2 = clxxMapper.selectOne(clxxQueryWrapper);
        if (clxx2 != null) {
            throw new JyException("该材料信息已入馆");
        }
        Integer typeId = clxx.getTypeId();
        Dalx dalx = dalxMapper.selectById(typeId);
        clxx.setTypeName(dalx.getDalxName());
        BeanUtils.copyProperties(clxx, clxxRg);
        clxxRgMapper.insert(clxxRg);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity queryDRg(User user, String sort, PageQuery pageQuery) {
        QueryWrapper<Rgsh> rgshQueryWrapper = new QueryWrapper<>();
        rgshQueryWrapper.eq("rgr_id", user.getId());
        rgshQueryWrapper.eq("zt", 1);
        if (sort.equalsIgnoreCase("desc")) {
            rgshQueryWrapper.orderByDesc("rgsj");
        }
//        Page<Rgsh> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page page = PageResultUtils.getPage(pageQuery, Rgsh.class);
        page = rgshMapper.selectPage(page, rgshQueryWrapper);
        PageResult pageResult = PageResultUtils.generatePageResult(page, pageQuery);
        return ResultEntity.ok(pageResult);
    }

    @Override
    public ResultEntity queryDSh(User user, String sort, PageQuery pageQuery) {
        QueryWrapper<Rgsh> rgshQueryWrapper = new QueryWrapper<>();
        rgshQueryWrapper.eq("shr_id", user.getId());
        rgshQueryWrapper.eq("zt", 2);
        if (sort.equalsIgnoreCase("desc")) {
            rgshQueryWrapper.orderByDesc("rgsj");
        }
        Page<Rgsh> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        page = rgshMapper.selectPage(page, rgshQueryWrapper);
        PageResult pageResult = PageResultUtils.generatePageResult(page, pageQuery);
        return ResultEntity.ok(pageResult);
    }

    @Override
    public ResultEntity queryDShCount(User user) {
        QueryWrapper<Rgsh> rgshQueryWrapper = new QueryWrapper<>();
        rgshQueryWrapper.eq("shr_id", user.getId());
        rgshQueryWrapper.eq("zt", 2);
        Integer count = rgshMapper.selectCount(rgshQueryWrapper);

        return ResultEntity.ok(count);
    }

    @Override
    public ResultEntity queryHsz(User user, String sort, PageQuery pageQuery) {
        QueryWrapper<Rgsh> rgshQueryWrapper = new QueryWrapper<>();
        rgshQueryWrapper.eq("zt", 3);
        if (sort.equalsIgnoreCase("desc")) {
            rgshQueryWrapper.orderByDesc("rgsj");
        }
        Page<Rgsh> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        page = rgshMapper.selectPage(page, rgshQueryWrapper);
        PageResult pageResult = PageResultUtils.generatePageResult(page, pageQuery);
        return ResultEntity.ok(pageResult);
    }

    @Override
    public ResultEntity queryRgDaxx(String dabh) throws UnsupportedEncodingException {
        String dacl = pathUtils.getPath();
        DaxxWsRg daxxWsRg = daxxWsRgMapper.findByDabh(dabh.trim());
        ArrayList list = new ArrayList<>();
        LxNode lxNode = new LxNode();
        lxNode.setId(dabh);
        lxNode.setText(dabh);
        list.add(lxNode);
//        String code = wsFlag.getCode(dabh);
//        Integer byCode = dalxMapper.findByCode(code);
        Integer byCode = wsFlag.getTypeId(dabh);
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer lx = hashMap.get(byCode);
        if (lx == 1) {
            List<LxNode> nodeList = getDaClxx(dabh, dacl);
            DaxxRg daxx = daxxRgMapper.findByDabh(dabh);
            lxNode.setNodes(nodeList);
            return ResultEntity.ok(new DaxxRgVo(daxx, list));
        } else if (lx == 2) {
            GryfKjRg gryfKjRg = gryfKjRgMapper.findByDabh(dabh);
            String path = dacl + "/" + gryfKjRg.getTypeName() + "/" + dabh;
            List<LxNode> nodes = getFile(path);
            lxNode.setNodes(nodes);
            return ResultEntity.ok(new DaxxRgVo(list, gryfKjRg));
        } else if (lx == 3) {
            DaxxWsRg daxxWs = daxxWsRgMapper.findByDabh(dabh);
            List<LxNode> nodeList = getDaClxx(dabh, dacl);
            lxNode.setNodes(nodeList);
            return ResultEntity.ok(new DaxxWsRgVo(daxxWs, list));
        }
        throw new JyException("档案类型不存在");
    }

    public List<LxNode> getDaClxx(String dabh, String dacl) throws UnsupportedEncodingException {
        ArrayList<LxNode> lxNodes = new ArrayList<>();
        List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(dabh);
        if (clxxRgs != null && clxxRgs.size() > 0) {
            for (int i = 0; i < clxxRgs.size(); i++) {
                ClxxRg clxx = clxxRgs.get(i);
                LxNode lxNode = new LxNode();
                lxNode.setId("/" + clxx.getDabh());
                lxNode.setText(clxx.getDabh());
                if (clxx.getQsym() == null) {
                    clxx.setQsym("000");
                }
                if (clxx.getZzym() == null) {
                    clxx.setZzym("000");
                }
                Integer qsym = Integer.valueOf(clxx.getQsym());
                Integer zzym = Integer.valueOf(clxx.getZzym());
//                ArrayList<LxNode> nodes = new ArrayList<>();
                LxNode lxNode1 = new LxNode();
                lxNode1.setId("/" + clxx.getWjxh() + clxx.getClmc());
                lxNode1.setText(clxx.getWjxh() + clxx.getClmc());
                ArrayList<String> yms = new ArrayList<>();
                for (int j = qsym; j <= zzym; j++) {
                    String buyema = Buling.Buyema(j);
                    yms.add(buyema);
                }
                String path = dacl + "/" + clxx.getTypeName() + lxNode.getId() + lxNode1.getId();
                File file = new File(path);
                if (file != null && file.isDirectory()) {
                    List<LxNode> nodeList = null;
                    try {
                        nodeList = getFile(path);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    lxNode1.setNodes(nodeList);
                    lxNodes.add(lxNode1);
                }
                String pdfPath = dacl + "/" + clxx.getTypeName() + lxNode.getId() + lxNode1.getId() + ".pdf";
                File file1 = new File(pdfPath);
                if (file1 != null && file1.isFile()) {
                    LxNode node = new LxNode(URLEncoder.encode("/" + clxx.getTypeName() + lxNode.getId() + "/" + file1.getName(), "utf-8"), file1.getName());
                    lxNodes.add(node);
                }
            }
        }
        return lxNodes;
    }

//    public List<LxNode> getDaGryfClxx(String dabh, String dacl) throws UnsupportedEncodingException {
//        ArrayList<LxNode> lxNodes = new ArrayList<>();
//        String path = dacl +"/" + dabh ;
//        File file = new File(path);
//        File[] files = file.listFiles();
//        if (files!=null && files.length>0){
//            for (File file1 : files) {
//                if (file1.isDirectory())
//            }
//        }
//        LxNode lxNode = new LxNode();
//        lxNode.setId("/" + dabh);
//        lxNode.setText(dabh);
////                ArrayList<LxNode> nodes = new ArrayList<>();
//        LxNode lxNode1 = new LxNode();
//
//        if (file != null && file.isDirectory()) {
//            List<LxNode> nodeList = null;
//            try {
//                nodeList = getFile(path);
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            lxNode1.setNodes(nodeList);
//            lxNodes.add(lxNode1);
//        }
//        String pdfPath = dacl + "/" + clxx.getTypeName() + lxNode.getId() + lxNode1.getId() + ".pdf";
//        File file1 = new File(pdfPath);
//        if (file1 != null && file1.isFile()) {
//            LxNode node = new LxNode(URLEncoder.encode("/" + clxx.getTypeName() + lxNode.getId() + "/" + file1.getName(), "utf-8"), file1.getName());
//            lxNodes.add(node);
//        }
//        return lxNodes;
//    }

    private List<LxNode> getFile(String path) throws UnsupportedEncodingException {
        ArrayList<LxNode> nodeList = new ArrayList<>();
        File file = new File(path);
        if (!file.exists()) {
//            throw new JyException("目录材料文件不存在，请联系管理员配置");
            return nodeList;
        }
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            if (file1.isDirectory()) {
//                throw new JyException("这里包含了文件夹");
                List<LxNode> list = getFile(file1.getPath());
                nodeList.add(new LxNode(file1.getName(), file1.getName(), list));
            } else {
                String name = file1.getName();
//                String suffix = name.substring(name.lastIndexOf(".") + 1);
                LxNode lxNode = new LxNode();
                lxNode.setText(name);
                lxNode.setId(URLEncoder.encode(file1.getPath(), "utf-8"));
                nodeList.add(lxNode);
            }
        }
        return nodeList;
    }


    @Override
    public ResultEntity kjxxExcleDr(ArrayList<DaxxRg> daxxes, User user, Directory d, IndexWriterConfig conf, IndexWriter indexWriter) throws IOException {
        for (DaxxRg daxxRg : daxxes) {
            Daxx daxx = new Daxx();
            BeanUtils.copyProperties(daxxRg, daxx);
            daxx.setYear(daxxRg.getYear()+"");
            daxxMapper.insert(daxx);
            syMoreMapper.insert(new SyMore(daxxRg.getDabh(), daxxRg.getInputtime(), daxxRg.getXmmc()));
            luceneUtiles.add(daxx, "kj", indexWriter);
        }
        indexWriter.commit();
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity wsxxExcleDr(ArrayList<DaxxWsRg> daxxWs, User user, Directory d, IndexWriterConfig conf, IndexWriter indexWriter) throws IOException {
        for (DaxxWsRg daxxWsRg : daxxWs) {
            DaxxWs daxxWs1 = new DaxxWs();
            BeanUtils.copyProperties(daxxWsRg, daxxWs1);
            daxxWs1.setNf(daxxWsRg.getNf() + "");
            daxxWs1.setNf(daxxWsRg.getNf()+"");
            daxxWsMapper.insert(daxxWs1);
            syMoreMapper.insert(new SyMore(daxxWsRg.getDabh(), daxxWsRg.getInputtime(), daxxWsRg.getWjmc()));
            luceneUtiles.add(daxxWs1, "ws", indexWriter);

            //            Rgsh rgsh = new Rgsh(daxxWsRg.getDabh(), new Date(), null, user.getId(), user.getName(), null, null, 1, 1);
            //            rgshMapper.insert(rgsh);
        }
        indexWriter.commit();
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity kjxxGryfExcleDr(ArrayList<GryfKj> gryfKjs, User user, Directory d, IndexWriterConfig conf, IndexWriter indexWriter) throws IOException {

        for (GryfKj gryfKj : gryfKjs) {
            gryfKjMapper.insert(gryfKj);
            syMoreMapper.insert(new SyMore(gryfKj.getDabh(), gryfKj.getInputtime(), gryfKj.getSqr()));
            luceneUtiles.add(gryfKj, "gr", indexWriter);

        }
        indexWriter.commit();
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity clxxExcleDr(ArrayList<ClxxRg> list, Directory d, IndexWriterConfig conf, IndexWriter indexWriter) throws IOException {

        for (ClxxRg clxxRg : list) {
            Clxx clxx = new Clxx();
            BeanUtils.copyProperties(clxxRg, clxx);
            clxxMapper.insert(clxx);
            luceneUtiles.add(clxx, "clxx", indexWriter);
        }
        indexWriter.commit();
        indexWriter.rollback();
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity submitRg(Integer id, Integer userId) {
        Rgsh rgsh = rgshMapper.selectById(id);
        User user = userMapper.selectById(userId);
        rgsh.setShrId(userId);
        rgsh.setShrName(user.getName());
        rgsh.setZt(2);
        rgshMapper.updateById(rgsh);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity rgaudit(Integer id, String rgyj, Integer flag, Directory d, IndexWriterConfig conf, IndexWriter indexWriter) throws IOException {
        Rgsh rgsh = rgshMapper.selectById(id);
        rgsh.setRgyj(rgyj);
        rgsh.setZt(flag);
        rgshMapper.updateById(rgsh);
        if (flag == 4) {
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(rgsh.getDabh());
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            for (ClxxRg clxxRg : clxxRgs) {
                clxxRg.setId(null);
                Clxx clxx = new Clxx();
                BeanUtils.copyProperties(clxxRg, clxx);
                clxxMapper.insert(clxx);
                luceneUtiles.add(clxx, "clxx", indexWriter);
                clxxRgMapper.deleteById(clxxRg.getId());
            }
            if (rgsh.getType() == 1) {
                DaxxRg byDabh = daxxRgMapper.findByDabh(rgsh.getDabh());
                Daxx daxx = new Daxx();
                BeanUtils.copyProperties(byDabh, daxx);
                daxx.setYear(byDabh.getYear() + "");
                //                daxx.setInputtime(new Date());
                daxxMapper.insert(daxx);
                syMoreMapper.insert(new SyMore(daxx.getDabh(), daxx.getInputtime(), daxx.getXmmc()));
                luceneUtiles.add(daxx, "kj", indexWriter);
                daxxRgMapper.deleteById(byDabh.getId());
            } else if (rgsh.getType() == 3) {
                DaxxWsRg byDabh = daxxWsRgMapper.findByDabh(rgsh.getDabh());
                DaxxWs daxxWs = new DaxxWs();
                BeanUtils.copyProperties(byDabh, daxxWs);
                daxxWs.setNf(byDabh.getNf() + "");
                daxxWsMapper.insert(daxxWs);
                syMoreMapper.insert(new SyMore(daxxWs.getDabh(), daxxWs.getInputtime(), daxxWs.getWjmc()));
                luceneUtiles.add(daxxWs, "ws", indexWriter);
                daxxRgMapper.deleteById(byDabh.getId());
            } else if (rgsh.getType() == 2) {
                GryfKjRg byDabh = gryfKjRgMapper.findByDabh(rgsh.getDabh());
                GryfKj gryfKj = new GryfKj();
                BeanUtils.copyProperties(byDabh, gryfKj);
                gryfKjMapper.insert(gryfKj);
                syMoreMapper.insert(new SyMore(gryfKj.getDabh(), gryfKj.getInputtime(), gryfKj.getSqr()));
                luceneUtiles.add(gryfKj, "gr", indexWriter);
                gryfKjRgMapper.deleteById(byDabh.getId());
            }
        }
        indexWriter.commit();
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity querycl(Integer id) {
        ClxxRg clxxRg = clxxRgMapper.selectById(id);
        return ResultEntity.ok(clxxRg);
    }

    @Override
    public void addClxxs(ArrayList<ClxxRg> clxxRgs) {
        for (ClxxRg clxxRg : clxxRgs) {
            clxxRgMapper.insert(clxxRg);
        }
    }

    @Override
    public ResultEntity deletedaRg(String dabh) {
//        String code = wsFlag.getCode(dabh);
//        Integer integer = dalxMapper.findByCode(code);
        Integer integer = wsFlag.getTypeId(dabh);
        HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
        Integer lx = hashMap.get(integer);
        if (lx == 3) {
            DaxxWsRg daxxWsRg = daxxWsRgMapper.findByDabh(dabh);
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(daxxWsRg.getDabh());
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            ArrayList<Integer> integers = new ArrayList<>();
            for (ClxxRg clxxRg : clxxRgs) {
                integers.add(clxxRg.getId());
            }
            clxxRgMapper.deleteBatchIds(integers);
            daxxWsRgMapper.deleteById(daxxWsRg.getId());

        } else if (lx.intValue() == 2) {
            GryfKjRg kjRg = gryfKjRgMapper.findByDabh(dabh);
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(kjRg.getDabh());
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            ArrayList<Integer> integers = new ArrayList<>();
            for (ClxxRg clxxRg : clxxRgs) {
                integers.add(clxxRg.getId());
            }
            if (integers.size() > 0) {
                clxxRgMapper.deleteBatchIds(integers);
            }
            gryfKjRgMapper.deleteById(kjRg.getId());
        } else if (lx.intValue() == 1) {
            DaxxRg daxxRg = daxxRgMapper.findByDabh(dabh);
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(daxxRg.getDabh());
            if (clxxRgs == null) {
                clxxRgs = new ArrayList<>();
            }
            ArrayList<Integer> integers = new ArrayList<>();
            for (ClxxRg clxxRg : clxxRgs) {
                integers.add(clxxRg.getId());
            }
            if (integers.size() > 0) {
                clxxRgMapper.deleteBatchIds(integers);
            }
            daxxRgMapper.deleteById(daxxRg.getId());
        }
        UpdateWrapper<Rgsh> wrapper = new UpdateWrapper<>();
        wrapper.eq("dabh", dabh);
        rgshMapper.delete(wrapper);
        return ResultEntity.ok();
    }

    public Analyzer getAnalyzer() {
        Analyzer analyzer = new Analyzer() {
            @Override
            protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
                Tokenizer source = new NGramTokenizer(reader, 1, 255);
                TokenStream filter = new StopFilter(source, new CharArraySet(0, false));
                return new TokenStreamComponents(source, filter);
            }
        };
        return analyzer;
    }


    @Override
    public ResultEntity queryDaxxNf(String keyword, Integer dalx) throws Exception {
        HashMap<Integer, Integer> dalxFlag = wsFlag.getDalxFlag();
        Integer integer = dalxFlag.get(dalx);
        ArrayList<String> list = new ArrayList<>();
        if (integer == 1) {
            list = daxxMapper.findGroupByYear(dalx);
        } else if (integer == 2) {
            list = gryfKjMapper.findGroupByYear(dalx);
        } else if (integer == 3) {
            list = daxxWsMapper.findGroupByYear(dalx);
        }
        ArrayList<DajsOuterCx> dajsOuterCxes = new ArrayList<>();
        Dalx select = dalxMapper.selectById(dalx);
        for (String s : list) {
            if (integer == 1) {
                Integer nf1 = luceneUtiles.searchDaxxNf(dalx, keyword, s);
                dajsOuterCxes.add(new DajsOuterCx(select.getDalxName(), s, nf1, dalx));
            } else if (integer == 2) {
                Integer nf1 = luceneUtiles.searchGryfNf(dalx, keyword, s);
                dajsOuterCxes.add(new DajsOuterCx(select.getDalxName(), s, nf1, dalx));
            } else if (integer == 3) {
                Integer nf1 = luceneUtiles.searchDaxxWsNf(dalx, keyword, s);
                dajsOuterCxes.add(new DajsOuterCx(select.getDalxName(), s, nf1, dalx));
            }
        }
        return ResultEntity.ok(dajsOuterCxes);
    }
}
